/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvAffectation;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvEquipedepartement;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvAffectationJpaController extends DaoImp implements Serializable {

    public CvAffectationJpaController() {
        super();
    }

    public void create(CvAffectation cvAffectation) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvEquipedepartement cvEquipedepartementIdequipedepartement = cvAffectation.getCvEquipedepartementIdequipedepartement();
            if (cvEquipedepartementIdequipedepartement != null) {
                cvEquipedepartementIdequipedepartement = em.getReference(cvEquipedepartementIdequipedepartement.getClass(), cvEquipedepartementIdequipedepartement.getIdequipedepartement());
                cvAffectation.setCvEquipedepartementIdequipedepartement(cvEquipedepartementIdequipedepartement);
            }
            em.persist(cvAffectation);
            if (cvEquipedepartementIdequipedepartement != null) {
                cvEquipedepartementIdequipedepartement.getCvAffectationList().add(cvAffectation);
                cvEquipedepartementIdequipedepartement = em.merge(cvEquipedepartementIdequipedepartement);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvAffectation cvAffectation) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvAffectation persistentCvAffectation = em.find(CvAffectation.class, cvAffectation.getIdaffectation());
            CvEquipedepartement cvEquipedepartementIdequipedepartementOld = persistentCvAffectation.getCvEquipedepartementIdequipedepartement();
            CvEquipedepartement cvEquipedepartementIdequipedepartementNew = cvAffectation.getCvEquipedepartementIdequipedepartement();
            if (cvEquipedepartementIdequipedepartementNew != null) {
                cvEquipedepartementIdequipedepartementNew = em.getReference(cvEquipedepartementIdequipedepartementNew.getClass(), cvEquipedepartementIdequipedepartementNew.getIdequipedepartement());
                cvAffectation.setCvEquipedepartementIdequipedepartement(cvEquipedepartementIdequipedepartementNew);
            }
            cvAffectation = em.merge(cvAffectation);
            if (cvEquipedepartementIdequipedepartementOld != null && !cvEquipedepartementIdequipedepartementOld.equals(cvEquipedepartementIdequipedepartementNew)) {
                cvEquipedepartementIdequipedepartementOld.getCvAffectationList().remove(cvAffectation);
                cvEquipedepartementIdequipedepartementOld = em.merge(cvEquipedepartementIdequipedepartementOld);
            }
            if (cvEquipedepartementIdequipedepartementNew != null && !cvEquipedepartementIdequipedepartementNew.equals(cvEquipedepartementIdequipedepartementOld)) {
                cvEquipedepartementIdequipedepartementNew.getCvAffectationList().add(cvAffectation);
                cvEquipedepartementIdequipedepartementNew = em.merge(cvEquipedepartementIdequipedepartementNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvAffectation.getIdaffectation();
                if (findCvAffectation(id) == null) {
                    throw new NonexistentEntityException("The cvAffectation with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvAffectation cvAffectation;
            try {
                cvAffectation = em.getReference(CvAffectation.class, id);
                cvAffectation.getIdaffectation();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvAffectation with id " + id + " no longer exists.", enfe);
            }
            CvEquipedepartement cvEquipedepartementIdequipedepartement = cvAffectation.getCvEquipedepartementIdequipedepartement();
            if (cvEquipedepartementIdequipedepartement != null) {
                cvEquipedepartementIdequipedepartement.getCvAffectationList().remove(cvAffectation);
                cvEquipedepartementIdequipedepartement = em.merge(cvEquipedepartementIdequipedepartement);
            }
            em.remove(cvAffectation);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvAffectation> findCvAffectationEntities() {
        return findCvAffectationEntities(true, -1, -1);
    }

    public List<CvAffectation> findCvAffectationEntities(int maxResults, int firstResult) {
        return findCvAffectationEntities(false, maxResults, firstResult);
    }

    private List<CvAffectation> findCvAffectationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvAffectation.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvAffectation findCvAffectation(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvAffectation.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvAffectationCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvAffectation> rt = cq.from(CvAffectation.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
